package com.hedl.classroom.test_class;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.mapping.*;
import co.elastic.clients.elasticsearch.core.IndexResponse;
import co.elastic.clients.elasticsearch.indices.CreateIndexResponse;
import com.alibaba.fastjson.JSON;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author Big Watermelon
 * @description
 * @date 2023/11/20/16:24
 */
@SpringBootTest
public class CreateElasticsearchMappings {

    @Resource
    private ElasticsearchClient client;

    @Value("${elasticsearch.apiKeys.course.index}")
    private String INDEX_NAME;

    @Test
    void createMappings() throws IOException {
        CreateIndexResponse createIndexResponse = client.indices().create(
                createIndexRequest -> createIndexRequest.index(INDEX_NAME)
                        .settings(
                                indexSettings -> indexSettings.numberOfShards("1").numberOfReplicas("0")
                        ).mappings(
                                typeMapping -> typeMapping
                                        .properties("id"
                                                , property -> property.keyword(KeywordProperty.of(pi -> pi)))
                                        .properties("companyId"
                                                , property -> property.keyword(KeywordProperty.of(pi -> pi)))
                                        .properties("companyName"
                                                , property -> property.text(TextProperty.of(pi -> pi.analyzer("ik_max_word").searchAnalyzer("ik_smart"))))
                                        .properties("name"
                                                , property -> property.text(TextProperty.of(pi -> pi.analyzer("ik_max_word").searchAnalyzer("ik_smart").copyTo("all"))))
                                        .properties("users"
                                                , property -> property.text(TextProperty.of(pi -> pi.index(false))))
                                        .properties("tags"
                                                , property -> property.text(TextProperty.of(pi -> pi.analyzer("ik_max_word").searchAnalyzer("ik_smart"))))
                                        .properties("mt"
                                                , property -> property.keyword(KeywordProperty.of(pi -> pi)))
                                        .properties("mtName"
                                                , property -> property.keyword(KeywordProperty.of(pi -> pi)))
                                        .properties("st"
                                                , property -> property.keyword(KeywordProperty.of(pi -> pi)))
                                        .properties("stName"
                                                , property -> property.keyword(KeywordProperty.of(pi -> pi)))
                                        .properties("grade"
                                                , property -> property.keyword(KeywordProperty.of(pi -> pi)))
                                        .properties("teachmode"
                                                , property -> property.keyword(KeywordProperty.of(pi -> pi)))
                                        .properties("pic"
                                                , property -> property.text(TextProperty.of(pi -> pi.index(false))))
                                        .properties("description"
                                                , property -> property.text(TextProperty.of(pi -> pi.analyzer("ik_max_word").searchAnalyzer("ik_smart").copyTo("all"))))
                                        .properties("createDate"
                                                , property -> property.date(DateProperty.of(pi -> pi.format("yyyy-MM-dd HH:mm:ss"))))
                                        .properties("status"
                                                , property -> property.keyword(KeywordProperty.of(pi -> pi)))
                                        .properties("remark"
                                                , property -> property.text(TextProperty.of(pi -> pi.index(false))))
                                        .properties("charge"
                                                , property -> property.keyword(KeywordProperty.of(pi -> pi)))
                                        .properties("price"
                                                , property -> property.scaledFloat(ScaledFloatNumberProperty.of(pi -> pi.scalingFactor(100.00))))
                                        .properties("originalPrice"
                                                , property -> property.scaledFloat(ScaledFloatNumberProperty.of(pi -> pi.scalingFactor(100.00))))
                                        .properties("validDays"
                                                , property -> property.integer(IntegerNumberProperty.of(pi -> pi)))
                                        .properties("all"
                                                , property -> property.text(TextProperty.of(pi -> pi.analyzer("ik_max_word"))))
                        )
        );
    }
}
